<% add_decidim_page_title(t(".title")) %>
<div class="card">
  <div class="item_show__header">
    <h1 class="item_show__header-title">
      <%= t ".title" %>
      <div class="flex ml-auto items-center gap-x-4">
        <strong class="font-bold text-md text-gray-2"><%= t("newsletters.index.subscribed_count", scope: "decidim.admin") %></strong>
        <span data-subscribed-count class="font-semibold text-md text-gray-2"><%= recipients_count_query %></span>
        <% if allowed_to? :create, :newsletter %>
          <%= link_to t("actions.newsletter.new", scope: "decidim.admin"), [:newsletter_templates], class: "button button__sm button__secondary new" %>
        <% end %>
      </div>
    </h1>
  </div>
  <div class="table-stacked">
    <table class="table-list">
      <thead>
        <tr>
          <th><%= t("models.newsletter.fields.subject", scope: "decidim.admin") %></th>
          <th><%= t("models.newsletter.fields.created_at", scope: "decidim.admin") %></th>
          <th><%= t("models.newsletter.fields.sent_at", scope: "decidim.admin") %></th>
          <th><%= t("models.newsletter.fields.sent_to", scope: "decidim.admin") %></th>
          <th><%= t("models.newsletter.fields.progress", scope: "decidim.admin") %></th>
          <th><%= t("decidim.admin.actions.actions") %></th>
        </tr>
      </thead>
      <tbody>
        <% @newsletters.each do |newsletter| %>
          <tr data-newsletter-id="<%= newsletter.id %>">
            <td data-label="<%= t("models.newsletter.fields.subject", scope: "decidim.admin") %>">
              <% if allowed_to?(:read, :newsletter, newsletter: newsletter) %>
                <%= link_to translated_attribute(newsletter.subject), decidim_admin.newsletter_path(newsletter) %>
              <% else %>
                <%= translated_attribute(newsletter.subject) %>
              <% end %>
            </td>

            <td data-label="<%= t("models.newsletter.fields.created_at", scope: "decidim.admin") %>">
              <%= l(newsletter.created_at, format: :decidim_short) if newsletter.created_at %>
            </td>

            <td data-label="<%= t("models.newsletter.fields.sent_at", scope: "decidim.admin") %>">
              <%= l(newsletter.sent_at, format: :decidim_short) if newsletter.sent_at %>
            </td>

            <td data-label="<%= t("models.newsletter.fields.sent_to", scope: "decidim.admin") %>">
              <%= selective_newsletter_to(newsletter) %>
            </td>

            <td data-label="<%= t("models.newsletter.fields.progress", scope: "decidim.admin") %>">
              <% if newsletter.sent? %>
                <%= "#{newsletter.total_deliveries} / #{newsletter.total_recipients}" %>
              <% end %>
            </td>

            <td data-label="<%= t("decidim.admin.actions.actions") %>" class="table-list__actions">
              <button type="button" data-controller="dropdown" data-target="actions-newsletter-<%= newsletter.id %>" aria-label="<%= t("decidim.admin.actions.actions_label", resource: t("models.newsletter.name", scope: "decidim.admin")) %>">
                <%= icon "more-fill", class: "text-secondary" %>
              </button>

              <div class="inline-block relative">
                <ul id="actions-newsletter-<%= newsletter.id %>" class="dropdown dropdown__action" aria-hidden="true">
                  <% if allowed_to?(:update, :newsletter, newsletter: newsletter) && !newsletter.sent? %>
                    <li class="dropdown__item">
                      <%= link_to decidim_admin.edit_newsletter_path(newsletter), method: :get, class: "dropdown__button" do %>
                        <%= icon "pencil-line" %>
                        <%= t("actions.edit", scope: "decidim.admin") %>
                      <% end %>
                    </li>

                    <hr>
                  <% end %>

                  <% if allowed_to?(:read, :newsletter, newsletter: newsletter) %>
                    <li class="dropdown__item">
                      <%= link_to decidim_admin.send_to_user_newsletter_path(newsletter), method: :post, class: "dropdown__button" do %>
                        <%= icon "share-line" %>
                        <%= t("actions.send_me_a_test_email", scope: "decidim.admin") %>
                      <% end %>
                    </li>

                    <hr>

                    <li class="dropdown__item">
                      <%= link_to decidim_admin.newsletter_path(newsletter), class: "dropdown__button", target: "_blank", data: { "external-link": false } do %>
                        <%= icon "eye-line" %>
                        <%= t("actions.preview", scope: "decidim.admin") %>
                      <% end %>
                    </li>
                  <% end %>

                  <% if allowed_to?(:destroy, :newsletter, newsletter: newsletter) && !newsletter.sent? %>
                    <hr>

                    <li class="dropdown__item">
                      <%= link_to decidim_admin.newsletter_path(newsletter), method: :delete, data: { confirm: t(".confirm_delete") }, class: "dropdown__button dropdown__button--danger" do %>
                        <%= icon "delete-bin-line" %>
                        <%= t("actions.destroy", scope: "decidim.admin") %>
                      <% end %>
                    </li>
                  <% end %>
                </ul>
              </div>
            </td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </div>
  <%= decidim_paginate @newsletters %>
</div>
